package com.xsmart.oa.dao;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 用户地址
 *
 * @author guojd
 */
@Component
public interface UserAddressDao {

    /**
     * 获取地区信息
     * @param regionId 父级地区id
     * @param regionType 地区类型，0国家，1省级，2地市，3区县
     * @return 地区列表
     */
    @Select("<script>select * from region where region_type=#{regionType} <if test=\"regionId!=null\"> and parent_id=#{regionId}</if></script>")
    List<Map<String,Object>> getAreaList(@Param("regionId") Integer regionId,@Param("regionType") int regionType);

    /**
     * 获取地区信息
     * @param regionId 地区id
     * @return 地区列表
     */
    @Select("<script>select * from region where region_id=#{regionId}</script>")
    Map<String,Object> getAreaInfo(@Param("regionId") Integer regionId);

    /**
     * 查询用户地址
     *
     * @param uid 用户id
     * @return 用户地址
     */
    @Select("<script>SELECT * FROM INFU_user_address WHERE user_id=#{uid}</script>")
    List<Map<String, Object>> queryUserAddress(@Param("uid") Integer uid);


    /**
     * 查询默认用户地址
     *
     * @param uid 用户id
     * @return 用户地址
     */
    @Select("<script>SELECT * FROM INFU_user_address WHERE user_id=#{uid} and is_default=1</script>")
    List<Map<String, Object>> querydefaultAddress(@Param("uid") Integer uid);

    /**
     * 设置默认
     *
     * @param uid            用户id
     * @param addressId      地址id
     * @return 影响记录数
     */
    @Update("<script>update INFU_user_address set is_default=#{isDefault} WHERE user_id=#{uid} <if test=\"addressId!=null\"> and address_id=#{addressId}</if></script>")
    int setDefault(@Param("uid") Integer uid, @Param("addressId") Integer addressId,@Param("isDefault") int isDefault);

    /**
     * 添加用户地址
     *
     * @param userAddressMap 用户地址
     * @return 影响记录数
     */
    @Insert("insert into INFU_user_address(user_id,consignee,email,country,province,city,district,street,address,zipcode," +
            "tel,mobile,sign_building,best_time,create_time,last_mod_time,full_address)" +
            " values(#{user_id},#{consignee},#{email},#{country},#{province},#{city},#{district},#{street},#{address},#{zipcode}," +
            "#{tel},#{mobile},#{sign_building},#{best_time},#{create_time},#{last_mod_time},#{full_address})")
    int addUserAddress(Map<String, Object> userAddressMap);

    /**
     * 修改用户地址
     * @param userAddressMap 用户地址
     * @return 影响记录数
     */
    @Update("update INFU_user_address set user_id=#{user_id},\n" +
            "consignee=#{consignee},\n" +
            "email=#{email},\n" +
            "country=#{country},\n" +
            "province=#{province},\n" +
            "city=#{city},\n" +
            "district=#{district},\n" +
            "street=#{street},\n" +
            "address=#{address},\n" +
            "zipcode=#{zipcode},\n" +
            "tel=#{tel},\n" +
            "mobile=#{mobile},\n" +
            "sign_building=#{sign_building},\n" +
            "best_time=#{best_time},\n" +
            "create_time=#{create_time},\n" +
            "last_mod_time=#{last_mod_time},\n" +
            "full_address=#{full_address} WHERE user_id=#{user_id} and address_id=#{addressId}")
    int modUserAddress(Map<String, Object> userAddressMap);

    /**
     * 删除用户地址
     *
     * @param uid       用户id
     * @param addressId 地址id
     * @return 影响记录数
     */
    @Delete("delete FROM INFU_user_address WHERE user_id=#{uid} and address_id=#{addressId}")
    int delUserAddress(@Param("uid") Integer uid, @Param("addressId") Integer addressId);
}
